<?php
/**
 * @author Etienne AUDOUIN <etienne.audouin@ingesup.com>
 * @author Adrien MARIANELLI <adrien.marianelli@ingesup.com>
 * @version 1.0
 * @package models
 * @subpackage newsletter
 */

class Newsletter_model extends CI_Model {

    /**
     * Reference a la table newsletter
     * @var string
     */
    protected $table = "newsletter";

    /**
     * Cree une newsletter
     * {@source}
     * @param string $content
     * @param string $mailinglists
     * @param string $title
     * @param string $signature
     * @return bool
     */
    public function create($content, $id_mailinglists, $title = null, $signature = null) {
        if ($title == null && $signature == null) {
            $this->db->set('content', $body);
            $this->db->set('id_mailinglists', implode(':', $id_mailinglists));
        } else {
            $this->db->set('title', $title);
            $this->db->set('content', $content);
            $this->db->set('signature', $signature);
            $this->db->set('id_mailinglists', implode(':', $id_mailinglists));
        }

        return $this->db->insert($this->table);
    }

    /**
     * Renvoie une newsletter
     * {@source}
     * @todo Nerrenvoie q'un resultat, placer un array_pop
     * @param int $id
     * @return array
     */
    public function read($id) {
        return $this->db->select('*')
                        ->from($this->table)
                        ->where('id', (int) $id)
                        ->get()
                        ->result();
    }

    /**
     * Renvoie toutes les newsletters
     * {@source}
     * @return array
     */
    public function readAll() {
        return $this->db->select('*')
                        ->from($this->table)
                        ->get()
                        ->result();
    }

    /**
     * Met a jour une newsletter
     * {@source}
     * @param string $mailinglists
     * @param string $signature
     * @param string $content
     * @param string $title
     * @param int $id
     * @return bool
     */
    public function update($mailinglists, $signature, $content = null, $title = null, $id = null) {
        if ($content == null || $title == null || $id == null)
            return false;

        $this->db->set('title', $title);
        $this->db->set('content', $content);
        
        if ($signature != null)
            $this->db->set('signature', $signature);
        if ($mailinglists != null)
            $this->db->set('id_mailinglists', implode(':', $mailinglists));

        $this->db->where('id', (int) $id);

        return $this->db->update($this->table);
    }

    /**
     * {@source}
     * Supprime une newsletter
     * @param int $id
     * @return bool
     */
    public function delete($id) {
        return $this->db->where('id', (int) $id)->delete($this->table);
    }
}
?>
